{extend name="common@create" /}

{block name="form"}
    <form class="layui-form layui-form-pane" lay-filter="myform">
          <div class="layui-form-item">
            <label for="school" class="layui-form-label"><span class="x-red">*</span>参考学校</label>
            <div class="layui-input-block">
              {php} $schoollist = schlist('校级','校级');{/php}
              <select name="school" id="school" lay-filter="school" lay-verify="required">
                <option value=""></option>
                {volist name="schoollist" id="vo"}
                  <option value="{$vo.id}"
                  {present name="$list.data.school"}
                    {eq name="$list.data.school" value="$vo.id" }
                      selected
                    {/eq}
                  {/present}>
                    {$vo.title}
                  </option>
                {/volist}
              </select>
            </div>
          </div>
          <div class="layui-form-item">
          		{volist name="$list.data.nianji" id="vo"}
	              <label for="title" class="layui-form-label">{$vo.title}</label>
	              <div class="layui-input-block">
	                  <input id="{$vo.id}" type="checkbox" title="全选" lay-skin="primary" action="parent">
	              </div>
                {php}
                  if($key==0){
                    $nianji = $vo['id'];
                  }else{
                    $nianji = $nianji.','.$vo['id'];
                  }
                {/php}
              {/volist}
              {php}
                if(empty($nianji)){ $nianji=''; }
              {/php}
          </div>
          <input type="hidden" name="kaoshi" id="kaoshi" value="{$list.set.kaoshi}">
          <div class="layui-form-item" style="text-align: right;">
              <button  class="layui-btn" lay-filter="mysubmit" lay-submit="">
                  {$list.set.butname}
              </button>
          </div>
      </form>
{/block}

{block name="myjs"}
<script>
  layui.use('form', function(){
    var form = layui.form;

    form.on('select(school)', function(data){
	  // 声明变量
	  var school = data.value;
      
      // 获取数据
        $.post(
            "/banji/schbanji",
            {
                "school":school
                ,"ruxuenian":"{$nianji}"
            },
            function(data,status){
              $('input[title="全选"]').each(function(){
                $(this).nextAll().remove();
              });
              $('input[title="全选"]').prop('checked',false);
                if($.isEmptyObject(data))
                {
                    return true;
                }
                for (var i in data) {
                  // $('#'+data[i]['ruxuenian']).nextAll().remove();
                	for(x in data[i]['nj_banji']){
                		$('#'+data[i]['ruxuenian']).parent().append('<input type="checkbox" name="banjiids[]" value="'+data[i]['nj_banji'][x]['id']+'" title="'+data[i]['nj_banji'][x]['banTitle']+'" lay-skin="primary" pid="'+data[i]['ruxuenian']+'" action="children">');
                	}
                }
                form.render('checkbox');
            }
        );

	}); 

	form.on('checkbox()', function(data){
      var myelem = data.elem
        ,ischecked = data.elem.checked
        ,action = $(myelem).attr('action')
        ,pid = $(myelem).attr('pid')
        ,id = $(myelem).attr('id');
      switch(action){
        case 'children':
          selectChildren(pid,ischecked);
          break;
        case 'parent':
          selectParent(id,ischecked);
          break;
      };
    });  

	// 更改子选项
	function selectChildren(pid,checked)
	{
		if(checked==true)
		{
			$('#'+pid).prop('checked',true);
			form.render('checkbox');
		}
	}
	// 更改父选项
	function selectParent(id,checked)
	{
		if(checked == true)
		{
			$("input[pid='"+id+"']").prop('checked',true);
		}else{
			$("input[pid='"+id+"']").prop('checked',false);
		}
		form.render('checkbox');

	}
     

  });

</script>
{/block}

